<div class="layuimini-main">

    <div class="layui-form layuimini-form">
        <div class="layui-form-item">
            <label class="layui-form-label required">科室编码：</label>
            <div class="layui-input-block">
                <input type="text" name="deptCode" lay-verify="required" lay-reqtext="科室编码不能为空" placeholder="请输入科室编码"
                    value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">科室名称：</label>
            <div class="layui-input-block">
                <input type="text" name="deptName" lay-verify="required" lay-reqtext="科室名称不能为空" placeholder="请输入科室名称"
                    value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">科室分类：</label>
            <div class="layui-input-block">
                <select name="deptCategoryId" lay-verify="required">
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">科室类型：</label>
            <div class="layui-input-block">
                <select name="deptTypeId" lay-verify="required">
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
        </div>
    </div>
</div>
<script>
    layui.use(['form', 'table'], function () {
        var form = layui.form,
            layer = layui.layer,
            table = layui.table,
            $ = layui.$;

        $.getJSON("/api/select/constItem.json", { constantTypeId: 1 }, function (data) {
            data.forEach(e => {
                $("select[name=deptCategoryId]").append("<option value='" + e.id + "'>" + e.name + "</option>");
            });
            form.render();
        });
        $.getJSON("/api/department/typeTable.json", function (data) {
            data.data.forEach(e => {
                $("select[name=deptTypeId]").append("<option value='" + e.id + "'>" + e.deptTypeName + "</option>");
            });
            form.render();
        });
        /**
        * 初始化表单，要加上，不然刷新部分组件可能会不加载
        */
        form.render();

        // 当前弹出层，防止ID被覆盖
        var parentIndex = layer.index;

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var index1 = layer.load(2);
            $.get("/api/department/add.json", data.field, function (data) {
                layer.close(index1);
                if (data.code == 0) {
                    var index = layer.alert(data.msg, {
                        title: '错误信息',
                        icon: 2
                    });
                } else {
                    layer.close(parentIndex);
                }
            }, "json");
            return false;
        });

    });
</script>